package org.hu.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.hu.entity.dto.OrderDetailAddDTO;
import org.hu.entity.dto.OrderDetailPageQueryDTO;
import org.hu.entity.dto.OrderPageQueryDTO;
import org.hu.entity.pojo.Order;
import org.hu.entity.pojo.OrderDetail;
import org.hu.entity.pojo.PageBean;
import org.hu.entity.vo.DailyStats;
import org.hu.entity.vo.MonthlyStats;
import org.hu.mapper.OrderMapper;
import org.hu.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    // 新增一个订单
    public void add(OrderDetailAddDTO orderDetailAddDTO) {
        OrderDetail orderDetail = new OrderDetail();
        BeanUtils.copyProperties(orderDetailAddDTO, orderDetail);
        // 补全订单的详细信息
        orderDetail.setOrderTime(LocalDateTime.now());
        // 计算单条订单的金额
        int amount = orderDetailAddDTO.getAmount();
        int count = orderDetailAddDTO.getCount();
        amount = amount * count;
        orderDetail.setAmount(amount);
        // 做判断,该桌号是否已经有人点单

        // 查订单细则表,获取该桌号下是否有未完成的订单
        List<OrderDetail> list = orderMapper.selectOrderByStatusAndTableId(orderDetailAddDTO);
        if(list.isEmpty()){
            // 要创建新订单
            // 先插入一条总订单,返回总订单的id作为订单详情的订单id
            orderMapper.insertOrder(orderDetail);
            // 插入一条订单详情
            orderMapper.insertOrderDetail(orderDetail);
            // 更新订单的总金额
            orderMapper.updateOrderAmountById(orderDetail);
        }else{

            // 获取所属订单的id
            Integer orderId = list.get(0).getId();
            orderDetail.setOrderId(orderId);
            // 说明不用创建新订单
            orderMapper.insertOrderDetail(orderDetail);
            // 更新订单的总金额
            orderMapper.updateOrderAmountById(orderDetail);
        }
    }

    // 删除一个订单
    public void delete(Integer id) {
        orderMapper.deleteOrderDetailById(id);
    }

    // 修改一个订单细则
    public void update(OrderDetail orderDetail) {
        orderMapper.updateOrderDetailById(orderDetail);
    }

    // 查询订单细则 列表
    public PageBean list(OrderDetailPageQueryDTO orderDetailPageQueryDTO) {
        // 获取分页参数
        PageHelper.startPage(orderDetailPageQueryDTO.getPageNum(), orderDetailPageQueryDTO.getPageSize());
        // 获取数据
        List<OrderDetail> list;
        Integer status = orderDetailPageQueryDTO.getStatus();
        if(status == null || !status.equals(7)){
            list  = orderMapper.listOrderDetail(orderDetailPageQueryDTO);
        }else{
            list = orderMapper.listUserOrderDetail(orderDetailPageQueryDTO);
        }
        // 将数据封装成分页对象
        Page<OrderDetail> page = (Page<OrderDetail>) list;
        // 封装为PageBean
        return new PageBean(page.getResult(), page.getTotal());
    }

    // 查询订单
    public PageBean listOrder(OrderPageQueryDTO orderPageQueryDTO) {
        // 获取分页参数
        PageHelper.startPage(orderPageQueryDTO.getPageNum(), orderPageQueryDTO.getPageSize());
        // 获取数据
        List<Order> list = orderMapper.listOrder(orderPageQueryDTO);
        // 将数据封装成分页对象
        Page<Order> page = (Page<Order>) list;
        return new PageBean(page.getResult(), page.getTotal());
    }

    // 修改订单
    public void updateOrderById(Order order) {
        if(order.getStatus()!= null){
            // 如果修改的信息包括订单状态的修改就把订单状态同步到对应的订单细则上
            Integer orderId = order.getId();
            Integer status = order.getStatus();
            orderMapper.updateOrderDetailStatusByOrderId(orderId, status);
        }
        orderMapper.updateOrder(order);
    }

    //  根据订单id修改菜品月售
    public void updateDishMonthSale(Integer id) {
        // 先根据订单id查询当前订单包含的菜品信息
        List<OrderDetail> list = orderMapper.selectOrderDetailById(id);
         for (OrderDetail orderDetail : list) {
            // 获取菜品id
            Integer dishId = orderDetail.getDishId();
            // 获取当前菜品销售量
             Integer count = orderDetail.getCount();
            // 修改菜品月售
            orderMapper.updateDishMonthSaleById(dishId, count);
        }


    }

    //  获取某年每个月的销售数据
    public List<MonthlyStats> getMonthlyStats(int year) {
        return orderMapper.getMonthlyStats(year);
    }

    //  获取某月每天的销售数据
    public List<DailyStats> getDailyStats(int year, int month) {
        return orderMapper.getDailyStats(year, month);
    }
}
